package com.qyhl.shop.shop.showcase;


import android.app.Activity;
import android.content.Context;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.shop.R;
import com.qyhl.shop.shop.shop.ShopDetailActivity;
import com.qyhl.webtv.basiclib.utils.SpanUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.entity.shop.ShopShowcaseBean;
import com.qyhl.webtv.commonlib.entity.shop.ShopShowcaseNormBean;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.Set;

import razerdp.basepopup.BasePopupWindow;

public class ShopShowcasePopupView extends BasePopupWindow {
    private View popupView;

    private TextView titleTv;
    private TextView normTv;
    private TextView priceTv;
    private TagFlowLayout tabLayout;
    private ImageButton cancelBtn;
    private RoundedImageView cover;

    public ShopShowcasePopupView(Activity context, ShopShowcaseBean bean) {
        super(context);
        bindEvent();
        init(context, bean);
    }

    private void init(Context context, ShopShowcaseBean bean) {
        titleTv.setText(bean.getItemName());

        Glide.with(context)
                .load(bean.getItemPic())
                .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default))
                .into(cover);

        setPrice(context, bean.getNormList().get(0));
        tabLayout.setMaxSelectCount(1);
        tabLayout.setAdapter(new TagAdapter<ShopShowcaseNormBean>(bean.getNormList()) {
            @Override
            public View getView(FlowLayout parent, int position, ShopShowcaseNormBean normBean) {
                TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.shop_item_showcase_popup_tag,
                        tabLayout, false);
                tv.setText(normBean.getText());
                return tv;
            }
        });

        tabLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                setPrice(context, bean.getNormList().get(position));
                return false;
            }
        });

        tabLayout.getAdapter().setSelectedList(0);


        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void setPrice(Context context, ShopShowcaseNormBean bean) {
        SpanUtils spUtils = new SpanUtils(context);
        spUtils.append("¥").setSpans(new TextAppearanceSpan(context, R.style.shop_showcase_list_price_one))
                .append(StringUtils.splitDouble(bean.getPrice(), true))
                .setSpans(new TextAppearanceSpan(context, R.style.shop_showcase_list_price_two))
                .append(StringUtils.splitDouble(bean.getPrice(), false))
                .setSpans(new TextAppearanceSpan(context, R.style.shop_showcase_list_price_one));

        priceTv.setText(spUtils.create());
        normTv.setText(bean.getText());
    }

    private void bindEvent() {
        titleTv = popupView.findViewById(R.id.title);

        normTv = popupView.findViewById(R.id.norm);

        priceTv = popupView.findViewById(R.id.price);

        tabLayout = popupView.findViewById(R.id.tabLayout);

        cancelBtn = popupView.findViewById(R.id.cancel_btn);

        cover = popupView.findViewById(R.id.cover);
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.shop_popup_window_showcase, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }
}
